import 'package:dio/dio.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:vischat/core/controller/auth_service.dart';
import 'package:vischat/data/datasources/remote/auth_remote.dart';
import 'package:vischat/data/models/user_model.dart';

class AuthRepository {
  final AuthRemote _authRemote;
  final AuthService _authService;

  AuthRepository(this._authRemote, this._authService);

  Future<UserModel> login({
    required LoginType loginType,
    String loginToken = '',
    String code = '',
    String email = '',
    String password = '',
  }) async {
    SmartDialog.showLoading();
    Response? response;
    switch (loginType) {
      case LoginType.sms:
        //response = await authRemote.smsLogin(loginToken);
        break;
      case LoginType.oneClick:
        //response = await authRemote.smsLogin(loginToken);
        break;
      case LoginType.code:
        break;
      case LoginType.email:
        break;
    }
    final Map<String, dynamic> testData = {
      "user": {"id": 1, "name": "测试用户", "userId": "snail"},
      "token_type": "",
      "access_token": "",
    };
    final userInfo = UserModel.fromJson(testData["user"]);

    final token_type = testData['token_type'];
    final access_token = testData['access_token'];

    // final userInfo = UserModel.fromJson(response?.data["user"]);
    //
    // final token_type = response?.data['token_type'];
    // final access_token = response?.data['access_token'];

    await _authService.setLoggedIn(userInfo, '$token_type $access_token');

    SmartDialog.dismiss();
    return userInfo;
  }

  Future<UserModel?> fetchUser({int? userId, bool isSelf = true}) async {
    if (isSelf) {
      return _authService.fetchUser();
    } else {
      Response response = await _authRemote.fetchUser(userId);
      return UserModel.fromJson(response.data);
    }
  }
}

enum LoginType { sms, oneClick, code, email }
